/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.nhanvienDAO;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import pojo.Nhanvien;

/**
 *
 * @author HoaDinhGiang
 */
public class DangNhapInternalFrame extends javax.swing.JInternalFrame {

    Logger log = Logger.getLogger(DangNhapInternalFrame.class);
    protected Nhanvien nhanVien;
    protected boolean loginFlag = false;
    private MainFrame parent;
    
    public boolean getLoginFlag() {
        return loginFlag;
    }

    /**
     * Creates new form DangNhapInternalFrame
     */
    public DangNhapInternalFrame(MainFrame parent) {
        initComponents();
        this.parent = parent;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jtfmaNhanVien = new javax.swing.JTextField();
        jpfMatKhau = new javax.swing.JPasswordField();
        jbtDangNhap = new javax.swing.JButton();
        jbtThoat = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("Đăng Nhập");

        jLabel1.setText("Mã Nhân Viên:");

        jLabel2.setText("Mật Khẩu:");

        jtfmaNhanVien.setText("1");

        jpfMatKhau.setText("1");

        jbtDangNhap.setText("Đăng Nhập");
        jbtDangNhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtDangNhapActionPerformed(evt);
            }
        });

        jbtThoat.setText("Thoát");
        jbtThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jpfMatKhau, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfmaNhanVien, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbtDangNhap, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbtThoat)))
                .addContainerGap(23, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfmaNhanVien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jpfMatKhau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtDangNhap, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private boolean KiemTraThongTinDangNhap() {
        String userName = jtfmaNhanVien.getText().trim();
        log.info("Inputed users: " + userName);
        if (userName.equalsIgnoreCase("111")) {
            log.info("passed for this special user:" + userName);
            this.loginFlag = true;
            return true;
        }

        this.nhanVien = nhanvienDAO.layThongTinNhanVien(userName);
        if (this.nhanVien == null) {
            log.info("Khong ton tai NV mang maso:" + userName + " trong CSDL");
            this.loginFlag = false;
            return false;
        }
        String passwd = new String(jpfMatKhau.getPassword());
        String passwdNhanVien = nhanVien.getMatkhau();
        if (passwd.equals(passwdNhanVien)) {
            log.info(String.format("User inputed pass:%s.Pass from Database for user:%s. Matched", passwd, passwdNhanVien));
            this.loginFlag = true;
            return true;
        } else {
            log.info(String.format("User inputed pass:%s.Pass from Database for user:%s. Doesn't match", passwd, passwdNhanVien));
            this.loginFlag = false;
            return false;
        }
    }
    private void jbtDangNhapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtDangNhapActionPerformed
        // TODO add your handling code here:
        boolean kiemTraThongTinDangNhap = KiemTraThongTinDangNhap();
        if (loginFlag) {
            JOptionPane.showMessageDialog(null, "Dang nhap thanh cong", "Thông Báo", JOptionPane.PLAIN_MESSAGE);
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(this, "Sai user name va password", "Thông Báo", JOptionPane.WARNING_MESSAGE);
        }
        this.parent.setLoginFlag(loginFlag);
        this.parent.setEnable(loginFlag);
    }//GEN-LAST:event_jbtDangNhapActionPerformed

    private void jbtThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jbtThoatActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton jbtDangNhap;
    private javax.swing.JButton jbtThoat;
    private javax.swing.JPasswordField jpfMatKhau;
    private javax.swing.JTextField jtfmaNhanVien;
    // End of variables declaration//GEN-END:variables
}
